package com.sunkey.annotation;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class CustomCondition implements Condition {  
    
    @Override  
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {  
        boolean isLocalBean = metadata.isAnnotated("com.sishuok.spring4.annotation.Local");  
        boolean isRemoteBean = metadata.isAnnotated("com.sishuok.spring4.annotation.Remote");  
        //如果bean没有注解@Local或@Remote，返回true，表示创建Bean  
        if(!isLocalBean && !isRemoteBean) {  
            return true;  
        }  
  
        boolean isLocalProfile = context.getEnvironment().acceptsProfiles("local");  
  
        //如果profile=local 且 bean注解了@Local，则返回true 表示创建bean；  
        if(isLocalProfile) {  
            return isLocalBean;  
        }  
  
        //否则默认返回注解了@Remote或没有注解@Remote的Bean  
        return isRemoteBean;  
    }  
}  